package com.dc.business.datacollectbusiness.service.impl;


import com.dc.business.datacollectbusiness.entity.User;
import com.dc.business.datacollectbusiness.mapper.UserMapper;
import com.dc.business.datacollectbusiness.service.CompanyUserRelationShipService;
import com.dc.business.datacollectbusiness.service.UserService;
import com.dc.business.datacollectbusiness.utils.Md5Util;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CompanyUserRelationShipService companyUserRelationShipService;

    @Override
    public User findUserByUsername(String userName) {
        return userMapper.selectUserByUsername(userName);
    }

    @Override
    public List<User> findAllUsers(int page, int size, String companyName,String phoneNumber) {
        int startIndex = (page - 1) * size;
        return userMapper.selectAllUser(startIndex,size,companyName,phoneNumber);
    }

    @Override
    public void insertUser(User user) {
        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        user.setPassword(Md5Util.md5(user.getPassword()));
        user.setCreateBy(map.get("username").toString());
        user.setUpdateBy(map.get("username").toString());
        userMapper.insertUser(user);
    }

    @Override
    public void updateUser(User user) {
        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        user.setPassword(Md5Util.md5(user.getPassword()));
        user.setUpdateBy(map.get("username").toString());
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUserByUserId(Integer userId) {
        userMapper.deleteUserByUserId(userId);
    }

    @Override
    public boolean deleteUserByIdall(User user) {
        boolean b=false;
        String arr[]=user.getArr();
        for (int i = 0; i < arr.length; i++) {
            int userId = Integer.parseInt(arr[i]);
            b=userMapper.deleteUserByUserId(userId);
            //如果删除成功,删除关系表信息
            if(b==true){
                b=companyUserRelationShipService.deleteCompanyUserRelationShipByuserID(userId);
            }
        }
        return b;
    }

    @Override
    public User getUserByid(int userId) {
        return userMapper.getUserByid(userId);
    }

    @Override
    public int getUseridByuserName(String userName) {
        return userMapper.getUseridByuserName(userName);
    }

    @Override
    public int getUsersumByCompanyName(String companyName) {
        return userMapper.getUsersumByCompanyName(companyName);
    }

    @Override
    public boolean upPasswd(String companyName, String userName, String newPassword) {
        boolean b=false;
        newPassword=Md5Util.md5(newPassword);
        b= userMapper.upPasswd(companyName,userName,newPassword);
        return b;
    }


}
